package cn.bjxy.shishaolong.ra_room.controller;

import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.ra_room.entity.RaroomFileRalation;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import cn.bjxy.shishaolong.system.file.entity.File;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 答疑室文件相关控制器
 *
 * @Author 时少龙
 * @Date 2019-04-20 14:02
 * @Version 1.0
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/web/raroom/file")
public class RaRoomFileController extends RaroomBaseController {

    /**
     * 查询答疑室文件  RoomId 为必填字段
     *
     * @return
     */
    @Permission
    @RequestMapping("/queryByCondition")
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            if (!param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                return retParamFailure();
            }
            File file = param.getObject(SysConst.QUERY_CONDITION_KEY, File.class);
            if (file.getRoomId() == null) return retParamFailure();

            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<File> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<File> list = raroomService.queryFileByCondition(page,file);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<File> list = raroomService.queryFileByCondition(file);
                return ResultMsg.success(list);
            }
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return retQueryFailure();
        }
    }

    /**
     * 文件上传
     *
     * @return
     */
    @RequestMapping("/upload")
    @Permission
    public ResultMsg fileUpload(HttpServletRequest request, @RequestBody RaroomFileRalation fileRalation) {
        try {
            if (fileRalation == null) {
                retParamFailure();
            }
            Integer currentUserId = getCurrentUserId(request);
            //如果答疑室未开通，不允许操作
            if (!isRoomOpen(currentUserId)) return retRoomStatusNotOpen();
            fileRalation.setRoomId(getRoomIdByTeacherId(currentUserId));
            raroomService.fileUpload(fileRalation);
            return retSuccess();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.OPERATOR_FAILURE, StatusCode.OPERATOR_FAILURE_MSG);
        }
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    @DeleteMapping("/delete")
    @Permission
    public ResultMsg delete(HttpServletRequest httRequest, @RequestBody JSONObject param) {
        try {
            if (param == null || !param.containsKey(SysConst.ID) || param.getString(SysConst.ID).isEmpty()) {
                return retParamFailure();
            }
            String id = param.getString(SysConst.ID);
            Integer userId = getCurrentUserId(httRequest);
            if (!isRoomOpen(userId)) retRoomStatusNotOpen();
            // 删除
            RaroomFileRalation fileRalation = new RaroomFileRalation().setRoomId(getRoomIdByTeacherId(userId))
                    .setFileId(Integer.parseInt(id));
            raroomService.remove(fileRalation);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.DELETE_FAILUER, StatusCode.DELETE_FAILUER_MSG);
        }
    }
}
